//
//  TRTools.m
//  DBManagerDemo
//
//  Created by CRY on 16/7/1.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import "TRTools.h"
#import <objc/runtime.h>

static NSString *const kAppVersion = @"TR_APP_VERSION";
NSDictionary *SQL_Type()
{
    static dispatch_once_t  once;
    static NSDictionary     *_type;
    dispatch_once(&once, ^() {
        _type = @{
                  @"T@\"NSString\"" : @"TEXT",
                  @"T@\"NSData\""   : @"BLOB",
                  @"T@\"NSDate\""   : @"DATE",
                  @"Tq"             : @"INTEGER",
                  @"Ti"             : @"INTEGER",
                  @"Tc"             : @"CHAR",
                  @"Tf"             : @"FLOAT",
                  @"Td"             : @"DOUBLE",
                  @"TB"             : @"BOOL"
                  };
    });
    return _type;
}

@implementation TRTools

+ (BOOL)isNewVersion{
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:kAppVersion];
    if ([app_Version isEqualToString:lastVersion]) {
        return NO;
    }else{
        [[NSUserDefaults standardUserDefaults] setValue:app_Version forKey:kAppVersion];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return YES;
    }
}

// 获取属性以及值
+ (NSDictionary *)properties_apsWithObject:(NSObject *)obj{
    
    if (obj == nil) return nil;
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([obj class], &outCount);
    for (i = 0; i < outCount; i++){
        
        objc_property_t property = properties[i];
        const char *char_f       = property_getName(property);
        NSString *propertyName   = [NSString stringWithUTF8String:char_f];
        id propertyValue         = [obj valueForKey:(NSString *)propertyName];
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}
// 获取属性以及类型
+ (NSDictionary *)properties_attrWithObject:(NSObject *)obj{
    
    if (obj == nil) return nil;
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([obj class], &outCount);
    for (i = 0; i < outCount; i++){
        
        objc_property_t property = properties[i];
        const char *char_f       = property_getName(property);
        const char *char_a       = property_getAttributes(property);
        NSString *propertyName   = [NSString stringWithUTF8String:char_f];
        NSString *propertyType   = [NSString stringWithUTF8String:char_a];
        [props setObject:propertyType forKey:propertyName];
    }
    free(properties);
    return props;
}
// 获取属性列表
+ (NSArray *)propertiesWithObject:(NSObject *)obj{
    
    if (obj == nil) return nil;
    u_int count;
    objc_property_t *properties     = class_copyPropertyList([obj class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++){
        
        const char* propertyName        = property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    return propertiesArray;
}
// 根据Objc属性类型获取相应sql存储类型
+ (NSString *)getSqlTypeWithAttribute:(NSString *)AttriString{
    
    NSString *type = [[AttriString componentsSeparatedByString:@","] firstObject];
    return SQL_Type()[type];
}

+ (NSString *)correctString:(NSString *)str{
    if (str.length == 0 ||str == nil) {
        return @"";
    }
    return str;
}

@end
